<script setup lang="ts">
import { ref } from 'vue'
import { mobileRules, passwordRules, codeRules } from '@/utils/rules'
import { showToast, type FormInstance } from 'vant'
import { useUserStore } from '@/stores'
// import router from '@/router' vue2的方式使用router
// vue3的方式使用router => 组合式api => useRouter() => router
// useRouter() router  useRoute() route  $route.query.xxx
// 只能在setup这一层使用 不能嵌套在其他函数中使用!!!
import { useRoute, useRouter } from 'vue-router'
import { getCode, loginByPassword, loginByMobile } from '@/services/user'
import { onUnmounted } from 'vue'
// import CpIcon from '@/components/CpIcon.vue'

const mobile = ref<string>('13230000001')
const password = ref<string>('abc12345')
const isAgree = ref<boolean>(true)

const route = useRoute()
console.log(route, 999)
const router = useRouter()

// 点击按钮 校验(???) 发请求 获取信息 存 跳转 提示
const fn = async () => {
  // button native-type="submit"  配合 form组件的submit事件
  // 这个函数是在整体校验通过过才会执行 =>
  if (!isAgree.value) return showToast('请同意')
  // console.log(111)
  const res = isPass.value
    ? await loginByPassword(mobile.value, password.value)
    : await loginByMobile(mobile.value, code.value)
  // console.log(res.data)
  useUserStore().setUser(res.data)
  router.push((route.query.backURL as string) || '/')
  // console.log(useRoute())
  showToast('登录成功')
}

// ----------------------------------短信登录----------------------------------
const isPass = ref<boolean>(false) // 表示当前表单的状态是否是密码登录
const code = ref<string>('')

const time = ref<number>(0)

const formRef = ref<FormInstance>() // formRef.value 存的是组件

let timeId: number // 这个数据不需要再template中展示 所有无需响应式

const send = async () => {
  // 倒计时期间不允许获取验证码
  if (time.value > 0) return

  // 得有手机号且是对的 才能获取验证码 => mobile字段单独校验 => 获取form组件validate('mobile')
  await formRef.value!.validate('xx')
  console.log('手机号满足条件')

  const res = await getCode(mobile.value, 'login')
  console.log(res.data.code)

  showToast('验证码已发送')

  // 开启倒计时
  time.value = 60

  timeId = setInterval(() => {
    time.value--
    console.log(time.value)

    if (time.value === 0) clearInterval(timeId)
  }, 1000)
}

// 组件卸载 如果定时器逻辑还在 需要清除
onUnmounted(() => {
  timeId && clearInterval(timeId)
})

const showPass = ref<boolean>(false) // 明文密文的数据
</script>

<template>
  <div class="login-page">
    <cp-nav-bar
      right-text="注册"
      @click-right="$router.push('/register')"
    ></cp-nav-bar>
    <!-- 头部 -->
    <div class="login-head">
      <h3>{{ isPass ? '密码登录' : '短信验证码登录' }}</h3>
      <a href="javascript:;" @click="isPass = !isPass">
        <span>{{ !isPass ? '密码登录' : '短信验证码登录' }}</span>
        <van-icon name="arrow"></van-icon>
      </a>
    </div>
    <!-- 表单 -->
    <van-form ref="formRef" @submit="fn" autocomplete="off">
      <van-field
        name="xx"
        v-model="mobile"
        placeholder="请输入手机号"
        type="tel"
        :rules="mobileRules"
      ></van-field>

      <van-field
        v-if="isPass"
        v-model="password"
        placeholder="请输入密码"
        :type="showPass ? 'text' : 'password'"
        :rules="passwordRules"
      >
        <template #button>
          <!-- <span @click="showPass = !showPass">
            {{ !showPass ? '👁👁' : '🙈🙈' }}
          </span> -->

          <cp-icon
            @click="showPass = !showPass"
            :name="showPass ? 'login-eye-off' : 'login-eye-on'"
          ></cp-icon>
          <!-- <cp-icon name="home-doctor"></cp-icon> -->
        </template>
      </van-field>

      <van-field
        v-else
        v-model="code"
        :rules="codeRules"
        placeholder="请输入验证码"
      >
        <!-- 具名插槽 button -->
        <template #button>
          <span class="btn-send" :class="{ active: time > 0 }" @click="send">
            {{ time > 0 ? `${time}秒后重新发送` : '发送验证码' }}
          </span>
        </template>
      </van-field>

      <div class="cp-cell">
        <van-checkbox v-model="isAgree">
          <span>我已同意</span>
          <a href="javascript:;">用户协议</a>
          <span>及</span>
          <a href="javascript:;">隐私条款</a>
        </van-checkbox>
      </div>
      <div class="cp-cell">
        <van-button native-type="submit" block round type="primary"
          >登 录</van-button
        >
      </div>
      <div class="cp-cell">
        <a href="javascript:;">忘记密码？</a>
      </div>
    </van-form>
    <!-- 底部 -->
    <div class="login-other">
      <van-divider>第三方登录</van-divider>
      <div class="icon">
        <img src="@/assets/qq.svg" alt="" />
      </div>
    </div>
  </div>

  <CpIcon name="consult-alipay"></CpIcon>
  <CpIcon name="home-article-default"></CpIcon>
  <CpIcon name="home-search"></CpIcon>
  <!-- <CpIcon :name="true"></CpIcon> -->

  <!-- 测试图标的基本使用 -->
  <!-- <svg aria-hidden="true">
    <use href="#icon-consult-alipay" />
  </svg>
  <svg aria-hidden="true">
    <use href="#icon-home-article-default" />
  </svg>
  <svg aria-hidden="true">
    <use href="#icon-home-search" />
  </svg>

  <SvgIcon></SvgIcon>
  <SvgIcon></SvgIcon>
  <SvgIcon></SvgIcon> -->
</template>

<style lang="scss" scoped>
.login {
  &-page {
    padding-top: 46px;
  }
  &-head {
    display: flex;
    padding: 30px 30px 50px;
    justify-content: space-between;
    align-items: flex-end;
    line-height: 1;
    h3 {
      font-weight: normal;
      font-size: 24px;
    }
    a {
      font-size: 15px;
    }
  }
  &-other {
    margin-top: 60px;
    padding: 0 30px;
    .icon {
      display: flex;
      justify-content: center;
      img {
        width: 36px;
        height: 36px;
        padding: 4px;
      }
    }
  }
}
.van-form {
  padding: 0 14px;
  .cp-cell {
    height: 52px;
    line-height: 24px;
    padding: 14px 16px;
    box-sizing: border-box;
    display: flex;
    align-items: center;
    .van-checkbox {
      a {
        color: var(--cp-primary);
        padding: 0 5px;
      }
    }
  }
  .btn-send {
    color: var(--cp-primary);
    &.active {
      color: rgba(22, 194, 163, 0.5);
    }
  }
}
</style>
